<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script src="jquery-3.0.0.min.js"></script>
		<script type="text/javascript">
			$(function () {
				ladderId = prompt("请输入打手id:","");
				if (ladderId != null && ladderId != '') {
					// var jsonObject = [];
					// var element = {"token":token,"activityId":activityId};
					// jsonObject.push(element);

					var wsServer = 'ws://101.200.166.70:9502';
					var websocket = new WebSocket(wsServer);

					websocket.onopen = function (evt) {
					    websocket.send(ladderId);
					    alert("WebSocket连接已建立");
					};

					websocket.onclose = function (evt) {
						websocket.send(JSON.stringify(jsonObject));
					    console.log("Disconnected");
					};

					websocket.onmessage = function (evt) {
						var data = JSON.parse(evt.data);
						console.log(data);
					    document.getElementById('progress').innerHTML = data['message'];
					};

					websocket.onerror = function (evt, e) {
					    console.log('Error occured: ' + evt.data);
					};

					$("button#test").click(function () {
						var score = $("input#score").val();
						$.ajax({
				            url: "http://123.56.78.190:9502",
				            type: "POST",
				            dateType: "json",
				            data: {
				            		token: token,
				            		activityId: activityId,
				            		score: score
				            	},
				            success: function (data) {
				            	if(data.errorCode != 200) {
				            		alert(data.errorMsg);return;
				            	}
				            	$.ajax({
						            url: "http://123.56.78.190/index.php/Api/Flashlight/getFlashlightInfo",
						            type: "POST",
						            dateType: "json",
						            data: {
						            		token: token,
						            		activityId: activityId,
						            	},
						            success: function (data) {
						            	if (data.errorCode == 200) {
											var trueData = data.data.rank_list;
											if (trueData != null && trueData != "") {
												var str = '<table border="1">'
															+'<thead>'
																+'<tr>'
																	+'<th>名次</th>'
																	+'<th>用户ID</th>'
																	+'<th>头像</th>'
																	+'<th>用户名</th>'
																	+'<th>贡献锋币</th>'
																+'</tr>'
															+'</thead>'
															+'<tbody>';
												for (var i = 0; i < trueData.length; i++) {
													str += '<tr>'
															+'<td>'+trueData[i].rank+'</td>'
															+'<td>'+trueData[i].user_id+'</td>'
															+'<td><img src="'+trueData[i].rank+'" style="width:100px;height:100px"></td>'
															+'<td>'+trueData[i].username+'</td>'
															+'<td>'+trueData[i].score+'</td>'
														  + '</tr>';
												}

												str += '</tbody>'
													+ '</table>';
											}

											$("div#rankList").html(str);
										}
						            }
						        });
				            }
				        });
					});
				}else{
					alert("你按了[取消]按钮或信息填写不全");
				}
			})
		</script>
	</head>

	<body>
		<div>
			当前进度：<input type="text" id="score">
		</div>
		<div>
			<button id="test">投币</button>
		</div>
		当前分数：<div id="progress"></div>
		排行榜：<br>
		<div id="rankList"></div>
	</body>

</html>


